/**
 * 
 */
package template.page.admin;

import java.util.List;

import org.apache.click.ActionListener;
import org.apache.click.Control;
import org.apache.click.control.AbstractLink;
import org.apache.click.control.ActionButton;
import org.apache.click.control.ActionLink;
import org.apache.click.control.Column;
import org.apache.click.control.PageLink;
import org.apache.click.control.Table;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.LinkDecorator;

import template.page.BorderPage;

/**
 * @author jaracje
 *
 */
@SuppressWarnings("serial")
public class ManageGameSessions extends BorderPage {
    
	public String title = "Manage Game Sessions";

	private Table phase1GameSessionTable = new Table("phase1GameSessionTable");
	private Table phase2GameSessionTable = new Table("phase2GameSessionTable");
	private Table closedGameSessionTable = new Table("closedGameSessionTable");
	private PageLink editGameSessionLink = new PageLink("Edit",EditGameSession.class);
	private ActionButton createGameSessionButton = new ActionButton("addGameSessionButton","Add Game Session");
	private ActionLink deleteGameSessionLink = new ActionLink("Delete",this,"onDeleteGameSessionClick");	
	private ActionLink goNextPhaseGameSessionLink = new ActionLink("goNextPhase",this,"onGoNextPhaseGameSessionClick");
	
	public ManageGameSessions() 
	{
		addControl(phase1GameSessionTable);
		addControl(phase2GameSessionTable);
		addControl(closedGameSessionTable);
		addControl(createGameSessionButton);
		addControl(deleteGameSessionLink);
		addControl(goNextPhaseGameSessionLink);
		
		// Setup table
		buildTable(phase1GameSessionTable);
		buildTable(phase2GameSessionTable);
		buildTable(closedGameSessionTable);
		
        createGameSessionButton.setTitle("Add Game Session");
		createGameSessionButton.setListener(this, "onAddGameSessionClick");

	}
	
	@SuppressWarnings("rawtypes")
	private void buildTable(final Table table) 
	{
		table.setClass(Table.CLASS_ITS);		
		table.setShowBanner(false);
		table.setSortable(false);
		
		table.addColumn(new Column("id"));		
		Column dateColumn = new Column("date");
		dateColumn.setFormat("{0,date,dd/MM/yyyy HH:mm}");
		table.addColumn(dateColumn);
		table.addColumn(new Column("playerCount"));
		table.addColumn(new Column("status"));
		
		editGameSessionLink.setImageSrc("/assets/images/actionEdit.gif");
		editGameSessionLink.setTitle("Edit Game Session");
        		
		deleteGameSessionLink.setImageSrc("/assets/images/actionDelete.gif");
		deleteGameSessionLink.setTitle("Delete Game Session");
		deleteGameSessionLink.setAttribute("onclick",
            "return window.confirm('Are you sure you want to delete this record?');");

		goNextPhaseGameSessionLink.setImageSrc("/assets/images/arrow.png");
		goNextPhaseGameSessionLink.setTitle("Go Next Phase");
		goNextPhaseGameSessionLink.setAttribute("onclick","return window.confirm('Are you sure ?');");
		
				
        Column column = new Column("Action");
        column.setTextAlign("center");
        AbstractLink[] links;
        if (closedGameSessionTable.equals(table)) 
        {
        	links = new AbstractLink[] { editGameSessionLink,deleteGameSessionLink };
        } else {
        	links = new AbstractLink[] { editGameSessionLink, goNextPhaseGameSessionLink, deleteGameSessionLink };
        }
        column.setDecorator(new LinkDecorator(table, links, "id"));
        column.setSortable(false);
        table.addColumn(column);
       

        // Table rowList will be populated through a DataProvider which loads
        // data on demand.
        table.setDataProvider(new DataProvider() {

            public List getData() {
                return getTableDataProvider(table);
            }
        });

        // Below we setup the table to preserve it's state (sorting and paging)
        // while editing customers
        table.getControlLink().setActionListener(new ActionListener() {

            public boolean onAction(Control source) {
                // Save Table sort and paging state between requests.
                // NOTE: we set the listener on the table's Link control which is invoked
                // when the Link is clicked, such as when paging or sorting.
                // This ensures the table state is only saved when the state changes, and
                // cuts down on unnecessary session replication in a cluster environment.
            	table.saveState(getContext()); 
                return true;
            }
        });

        // Restore the table sort and paging state from the session between requests
        table.restoreState(getContext()); 
	}
	
	@SuppressWarnings("rawtypes")
	private List getTableDataProvider(Table table) 
	{		
		if (phase1GameSessionTable.equals(table)) 
		{
			return gameSessionService.getPhase1GameSessions();   
		} else if (phase2GameSessionTable.equals(table)) 
		{
            return 	gameSessionService.getPhase2GameSessions();        
		} else {
			return gameSessionService.getClosedGameSessions();
		}
	}
	/**
     * Handle the delete row click event.
     */    
    public boolean onDeleteGameSessionClick() {
        Long id = deleteGameSessionLink.getValueLong();
        getGameSessionService().deleteGameSession(id);
        return true;
    }
    
	/**
     * Handle the delete row click event.
     */    
    public boolean onAddGameSessionClick() {
    	setRedirect(EditGameSession.class);        
        return true;
    }
    
    /**
     * Handle the delete row click event.
     */    
    public boolean onGoNextPhaseGameSessionClick() {
        Long id = goNextPhaseGameSessionLink.getValueLong();
        getGameSessionService().goNextPhase(id);
        return true;
    }
    
}
